import time
from queue import Queue
from concurrent.futures import ThreadPoolExecutor


def get_detail_html(queue: Queue):
    while True:
        while not queue.empty():
            url = queue.get()
            if url is None:
                queue.task_done()  # 标记任务完成
                break  # 收到结束信号，退出循环
            print(f"get detail html start: {url}")
            time.sleep(1)
            print(f"get detail html done: {url}")


def get_detail_url(queue: Queue):
    print(f"get detail url start")
    time.sleep(1)
    for i in range(2):
        queue.put(f"https://www.baidu.com/detail_{i}")
    print("get detail url done")
    queue.put(None)  # 发送结束信号


if __name__ == "__main__":
    queue = Queue()

    with ThreadPoolExecutor(max_workers=3) as executor:
        # 提交多个 get_detail_url 任务
        for _ in range(3):
            executor.submit(get_detail_url, queue)

        # 提交多个 get_detail_html 任务
        for _ in range(2):
            executor.submit(get_detail_html, queue)

    queue.join()  # 等待所有任务完成
    print("All tasks are done.")
